package  
{
	import com.bit101.components.FPSMeter;
	import com.bit101.components.PushButton;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.display.StageDisplayState;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.geom.Point;
	/**
	 * ...
	 * @author Ivan
	 */
	public class TilemapTest extends Sprite
	{
		//[Embed(source='../../prj/iStar/Docs/Art Docs/mapeditor/map_debug.tmx', mimeType='application/octet-stream')]
		[Embed(source='../../prj/iStar/Docs/Art Docs/mapeditor/ORTOGONAL/map_cuad_1024x520.tmx', mimeType='application/octet-stream')]
		//[Embed(source='../../prj/iStar/Docs/Art Docs/mapeditor/mapa_007.tmx', mimeType='application/octet-stream')]
		//[Embed(source='../../prj/iStar/Docs/Art Docs/mapeditor/mapa_003.tmx', mimeType='application/octet-stream')]
		private var rMap:Class;
		
		public function TilemapTest() 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			
			tilemap = new TileMap(new XML(new rMap));
			// tilemap.y = -600;
			addChild(tilemap)
			
			for (var i:int = 0; i < 250; i++)
			{
				var boid:Boid = new Boid(i);
				tilemap.addChild(boid);
				boids.push(boid);
			}
			
			new Scroller(tilemap);
			new FPSMeter(this, 0, 0);
			// new PushButton(this, 10, 30, "Full Screen", onFullScreen);
			
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
			addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			
			// TODO Test graphics behaviour.
			/*
			var bmp:BitmapData = new BitmapData(32, 32, false, 0xFF0000);
			var sprite:Sprite = new Sprite();
			var matrix:Matrix = new Matrix();
			matrix.translate(8, 8);
			sprite.graphics.beginBitmapFill(bmp, matrix, false);
			sprite.graphics.drawCircle(100, 100, 50);
			sprite.graphics.endFill();
			addChild(sprite);*/
		}
		
		private function onMouseDown(e:MouseEvent):void
		{
			Boid.target = new Point(e.localX, e.localY);
		}
		
		private function onEnterFrame(e:Event):void
		{
			for (var i:int = 0; i < boids.length; i++)
			{
				var boid:Boid = boids[i];
				boid.update(boids, i);
			}
		}
				
		private function onFullScreen(e:Event):void 
		{
			if (stage.displayState == StageDisplayState.NORMAL)
				stage.displayState = StageDisplayState.FULL_SCREEN;
			else
				stage.displayState = StageDisplayState.NORMAL;
		}
		
		private var tilemap:TileMap;
		private var boids:Vector.<Boid> = new Vector.<Boid>();
		
	}

}